/**
 * 
 */
package com.fantabest.frontend.client.mvc;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.fantabest.common.client.AppEvents;
import com.fantabest.common.client.RegistryKeys;
import com.fantabest.common.entity.Manager;
import com.fantabest.frontend.client.widget.LoginWindow;
import com.fantabest.frontend.client.widget.MainMenu;
import com.fantabest.frontend.client.widget.MarketPanel;
import com.fantabest.frontend.client.widget.NorthContainer;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author giant
 * 
 */
public class AppView extends View {

	private Viewport viewPort;
	private NorthContainer north;
	private MainMenu west;
	private LayoutContainer center;

	public AppView(Controller controller) {
		super(controller);
	}

	private void initUI() {
		viewPort = new Viewport();
		viewPort.setLayout(new BorderLayout());

		north = new NorthContainer();
		north.setEnabledTopMenu(false);
		west = new MainMenu();
		center = new LayoutContainer(new FitLayout());

		Registry.register(RegistryKeys.NORTH_CONTAINER, north);
		Registry.register(RegistryKeys.WEST_CONTAINER, west);
		Registry.register(RegistryKeys.CENTER_PANEL, center);
		Registry.register(RegistryKeys.VIEWPORT, viewPort);

		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH,
				100);
		northData.setCollapsible(false);
		northData.setFloatable(false);
		northData.setHideCollapseTool(true);
		northData.setSplit(false);
		northData.setMargins(new Margins(0, 0, 0, 0));

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 5, 15));

		viewPort.add(north, northData);
		viewPort.add(center, centerData);

		RootPanel.get().add(viewPort);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI();
		} else if (event.getType() == AppEvents.Login) {
			handleLogin(event);
		} else if (event.getType() == AppEvents.Logoff) {
			handleLogoff(event);
		} else if (event.getType() == AppEvents.GoToLogin) {
			handleGoToLogin(event);
		} else if (event.getType() == AppEvents.MenuMarket) {
			handleMenuMarket(event);
		}
	}

	private void handleLogin(AppEvent event) {
		Manager manager = ((BeanModel)event.getData()).getBean();
		if (manager != null) {
			Registry.register(RegistryKeys.MANAGER_LOGGED_IN, manager);
			LoginWindow loginDialog = Registry.get(RegistryKeys.LOGIN_DIALOG);
			loginDialog.hide();
			
			BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200);
			westData.setSplit(true);
			westData.setCollapsible(true);
			westData.setMaxSize(241);
			westData.setMinSize(0);
			westData.setMargins(new Margins(10, 10, 10, 10));
			viewPort.add(west, westData);
			
			north.getLoginTopBar().logManagerIn();
			north.setEnabledTopMenu(true);
			
			viewPort.layout(true);
		} else {
			LoginWindow loginDialog = Registry.get(RegistryKeys.LOGIN_DIALOG);
			loginDialog.setStatusMessage("Sigin failed!");
		}
	}

	private void handleLogoff(AppEvent event) {
		Registry.unregister(RegistryKeys.MANAGER_LOGGED_IN);
		north.getLoginTopBar().logManagerOff();
	}

	private void handleGoToLogin(AppEvent event) {
		LoginWindow loginDialog = new LoginWindow();
		Registry.register(RegistryKeys.LOGIN_DIALOG, loginDialog);
		loginDialog.show();
	}
	
	private void handleMenuMarket(AppEvent event) {
		Viewport viewport = Registry.get(RegistryKeys.VIEWPORT);
		LayoutContainer center = Registry.get(RegistryKeys.CENTER_PANEL);
		center.removeAll();
		center.setLayout(new FitLayout());
		center.add(new MarketPanel());
		viewport.layout(true);
	}
}
